<style lang="scss" scoped="">
  @import '../../styles/variables';


</style>
<template>
  <div>
    <div style="padding:10px;">
      <template>
        <el-table
          :data="nodeList"
          border
          style="width: 100%">
          <el-table-column
            prop="ip"
            label="IP">
          </el-table-column>
          <el-table-column
            label="操作">
            <template slot-scope="scope">
              <el-button icon="el-icon-t-edit" type="primary" size="mini" circle plain
                         @click.stop="editItem(scope.row)"></el-button>
              <el-button icon="el-icon-t-remove" type="danger" size="mini" circle plain
                         @click.stop="removeItem(scope.row)"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="1"
            :page-sizes="[10, 50, 100]"
            :page-size="filter.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="nodeCount">
          </el-pagination>
        </div>
      </template>
    </div>
  </div>
</template>
<script>
  import utils from '@/utils/utils';
  import {whiteListService} from '@/api/index';
  import {mapGetters} from 'vuex';

  export default {
    data() {
      return {
        filter: {
          pageNo: 1,
          pageSize: 10,
          start: 0
        },
        nodeCount: 0,
        nodeList: []
      };
    },
    computed: {
      addTrigger() {
        return this.$store.getters.addTrigger;
      }
    },
    watch: {
      'filter': {
        handler: function () {
          this.getList(false);
        },
        deep: true,
        immediate: true
      },
      addTrigger(value) {
        if (value) {
          this.addItem();
        }
      }
    },
    components: {},
    methods: {
      handleCurrentChange(currentPageNo) {
        this.filter.start = (currentPageNo - 1) * this.filter.pageSize;
      },
      handleSizeChange(pageSize) {
        this.filter.pageSize = pageSize;
        localStorage.setItem('whitelist-page-size', pageSize);

      },
      getList(isRefresh = true) {
        if (isRefresh === true) {
          this.filter.start = 0;
        }
        whiteListService.getPage(this.filter).then(res => {
          this.nodeCount = res.data.total;
          this.nodeList = res.data.rows;
        });
      },
      doSaveItem(item) {
        this.$prompt('请输入IP', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /^[0-9\.]+$/,
          inputErrorMessage: 'IP格式不正确',
          inputValue: item.ip
        }).then(({value}) => {
          this.$store.commit('ADD_TRIGGER', false);
          if (item.id) {
            whiteListService.update(Object.assign(item, {ip: value})).then(res => {
              this.$message('修改成功');
            });
          } else {
            whiteListService.add({ip: value}).then(res => {
              this.getList();
            });
          }
        });
      },
      editItem(item) {
        this.doSaveItem(item);
      },
      addItem(item) {
        this.doSaveItem({ip: ''});
      },
      doDel(ids) {
        this.$confirm('是否确认删除', '', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          whiteListService.delete({id: ids}).then(res => {
            this.getList();
          });
        });
      },
      removeItem(item) {
        this.doDel(item.id);
      }
    },
    created() {
      let oldPageSize = localStorage.getItem('whitelist-page-size');
      if (!oldPageSize) {
        oldPageSize = 10;
      }
      this.filter.pageSize = Math.floor(oldPageSize);
    }
  };

</script>
